// pages/perfectData/perfectData.js
const app = getApp()
var CONFIG = require("../../config/config.js");
var share = require('../../utils/share.js');
var list = [];
Page({
  data: {
    index: 0,
    consultant_index: 0,   //选择课程顾问的下标
    consultants: [],  //所有的课程顾问
    consultant: [],   //选择课程顾问的详细信息
    agent_index: 0,   //代理商
    agents: [],
    userSex: 1,
    region: ['', ''],   //地址
    user: {},
    showConsultant: true,
    multiIndex: [0, 0],
    multiArray: CONFIG.MULTIARRAY,
    objectMultiArray: CONFIG.OBJECTMULTIARRAY,
    phone:'',
  },


  // 地址
//   bindMultiPickerChange: function (e) {
//     // console.log(this.data.multiArray[0][this.data.multiIndex[0]]);
//     // vm = this;
//     this.setData({
//       "multiIndex[0]": e.detail.value[0],
//       "multiIndex[1]": e.detail.value[1]
//     })
//     this.setData({
//       region: [this.data.multiArray[0][this.data.multiIndex[0]], this.data.multiArray[1][this.data.multiIndex[1]]]
//     })
//   },

//   bindMultiPickerColumnChange: function (e) {
//     // vm = this;
//     var that = this;
//     switch (e.detail.column) {
//       case 0:
//         list = []
//         for (var i = 0; i < that.data.objectMultiArray.length; i++) {
//           if (that.data.objectMultiArray[i].parid == that.data.objectMultiArray[e.detail.value].regid) {
//             list.push(that.data.objectMultiArray[i].regname)
//           }
//         }
//         that.setData({
//           "multiArray[1]": list,
//           "multiIndex[0]": e.detail.value,
//           "multiIndex[1]": 0
//         })

//     }
//   },



  // 课程顾问
//   bindConsultantChange: function (e) {
//     var consultant_index = e.detail.value;
//     // console.log(this.data.consultants);
//     var consultant = this.data.consultants[consultant_index];
//     // console.log(this.data.consultants);
//     this.setData({
//       consultant: consultant,
//       consultant_index: consultant_index
//     })
//   },

  // 性别
  userSexChange: function (e) {
    this.setData({
      userSex: parseInt(e.detail.value)
    })
  },


    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        
        var uid = app.globalData.uid;
        // console.log(uid);
        // console.log('onLoad:' + uid);
        // 获取信息函数
        var phone = wx.getStorageSync('tel');
        this.setData({
            phone:phone, 
        })
        this.getCrmUser();
        // console.log(this.data.region);
        // 获取代理商和课程顾问
        // this.getAgentsAndConsultants();

    },


  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.getCrmUser();
    // this.getAgentsAndConsultants();
    wx.stopPullDownRefresh();
  },

  // 用户点击右上角分享

  onShareAppMessage: function () {
    return {
      title: CONFIG.SHARETITLE,
      path: CONFIG.PATHLOGIN
    }
  },

  /**
   * 查询客户信息
   */
  getCrmUser: function () {
    var uid = app.globalData.uid;
    // console.log(uid);
    wx.showLoading({
      title: '加载中',
      mask: true
    })
    let that = this;
    var phone = that.data.phone;
    //获取绑定信息
    share.wxRequest('crmapi/Student/stuInfo', { phone: phone}, function (res) {
        // console.log(res);
        if (Number(res.data.errcode) == 0){
            var data = res.data.data;
            that.setData({
                user: data
            })
        }
        wx.hideLoading();
    })
  },

  //获取课程顾问信息和代理商
//   getAgentsAndConsultants: function () {
//     wx.showLoading({
//       title: '加载中',
//       mask: true
//     })
//     let that = this;
//     wx.request({
//       url: CONFIG.API_HOST + 'crm/public/getAgentsAndConsultants',
//       method: 'GET',
//       success: function (res) {
//         // console.log(res);
//         that.setData({
//           agents: res.data.agents,  //代理商
//           consultant: res.data.consultants[0],  //默认选择第一个
//           consultants: res.data.consultants   //课程顾问
//         });
//         if (that.data.consultants.length != 0) {
//           wx.hideLoading();
//         }
//       },
//       fail: function (res) {
//         wx.startPullDownRefresh();
//         wx.hideLoading();
//         wx.showModal({
//           title: CONFIG.MODEL_TITLE,
//           showCancel: false,
//           content: CONFIG.REQUEST_FAIL,
//           confirmText: '点击重试',
//           success: function (res) {
//             wx.navigateBack();
//           }
//         })
//       }
//     })
//   },


  // 点击提交
  bindFormSubmit: function (e) {
    var e1 = e.detail.value;
    // console.log(e);
    var uid = app.globalData.uid;
    if (this.data.user.consultant_id) {
      var consultant_id = this.data.user.consultant_id;
    }
    if (e1.truename == '') {
        wx.showToast({
            title: '请输入您的姓名',
            icon: 'none',
            duration: 1500
        })
    } else if (!(/^[\u2E80-\u9FFF]+$/.test(e1.truename))) {
        wx.showToast({
            title: '姓名只能为汉字',
            icon: 'none',
            duration: 1500
        })
    }  else if (!(/^1(3|4|5|6|7|8|9)\d{9}$/.test(e1.tel)) || e1.tel == '') {
        wx.showToast({
            title: '请填写正确手机号',
            icon: 'none',
            duration: 1500
        })
    }
    //  else if (this.data.region[0] == '') {
    //     wx.showToast({
    //         title: '请选择地址',
    //         icon: 'none',
    //         duration: 1500
    //     })
    // } 
    else {
    //   if (!consultant_id) {
    //     consultant_id = this.data.consultant.admin_id;
    //   }

      wx.showLoading({
        title: '正在绑定~',
        mask: true
      })
      var params = {
            name: e1.truename,
            phone: e1.tel,
            sex: this.data.userSex,
        // agent_id: agent_id,
        // consultant_id: consultant_id,
        // region: this.data.region[0],
        // address: this.data.region[0] + ',' + this.data.region[1],
        // id: uid
      }
      // console.log(params); 
    share.wxRequest('crmapi/Student/editStuInfo', params, function (res) {
          wx.hideLoading();
          wx.showModal({
            title: CONFIG.MODEL_TITLE,
            content: res.data.msg,
            complete: function () {
              wx.navigateBack();
            }
          })
      })
    }

  }
})